<template>
  <div>
     <div>
        <el-row class="">
          <el-col class="">
            <el-form ref="form"  label-position='right' size="small" :inline="true" label-width="110px">
              <el-col :span="5" >
                  <el-form-item label="" prop="name" size='small'  style="margin:12px 5px;">
                      <el-input prefix-icon="el-icon-search" placeholder="请输入登录名" v-model="listQueryAll.condition.user_loginName.value" clearable></el-input>
                  </el-form-item>
              </el-col>
              <el-col :span="5" >
                  <el-form-item label="" prop="name" size='small'  style="margin:12px 5px;">
                      <el-input prefix-icon="el-icon-search" placeholder="请输入用户名" v-model="listQueryAll.condition.user_name.value" clearable></el-input>
                  </el-form-item>
              </el-col>
              <el-col :span="6" style="padding-left:5px;margin-top:12px;float:left;width:200px" algin="left">
                  <el-button size="small" style="height:30px;padding-top:0px;line-height:30px;" type="success" icon="el-icon-edit" @click="getList('find')">查 询</el-button>
                  <el-button size="small" style="height:30px;padding-top:0px;line-height:30px;" type="danger" icon="el-icon-edit" @click="handleReset()">重 置</el-button>
                 </el-col>
              </el-form>
            </el-col>

        </el-row>
        <el-row>
          <el-col :span="24">
            <el-table
              :data="allUserData"
              size="small"
              height="275px"
              @select="selectionChange"
              @select-all="selectionChange"
              @selection-change="selectionValChange"
              style="width: 100%;overflow: auto;border:#eeeeee">
              <el-table-column
                type="selection"
                width="120" align="center" >
              </el-table-column>
              <el-table-column type="index" label="序号" width="50">
                <template slot-scope="scope">
                <span>{{scope.$index + (serialNumberAll - 1) * 10 + 1}}</span>
                </template>
              </el-table-column>
              <el-table-column
                prop="user_loginName"
                label="登录名"
                width="120" align="center" >
              </el-table-column>
              <el-table-column
                prop="user_name"
                label="用户名称"
                width="150" align="center" >
              </el-table-column>
              <el-table-column
                prop="user_orgName"
                label="组织机构" align="center" >
              </el-table-column>
            </el-table>
            <div class="pagination-container">
              <el-pagination background @size-change="handleSizeChangeBatch"
                             @current-change="handleCurrentChangeBatch"
                             :page-sizes="[10,20,30, 50]"
                             layout="total, sizes, prev, pager, next, jumper"
                             :total="allTotal">
              </el-pagination>
            </div>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="24">
            <el-button size="small" @click="closeSelf" style="float:right;margin-right: 10px;" type="primary">关 闭</el-button>
            <el-button size="small" @click="insertList('insert')" style="float:right;margin-right: 10px;" type="primary">保存</el-button>
          </el-col>
        </el-row>
     </div>
  </div>
</template>

<script>
import {
  compositeSelect,
  batchInsertItemUser
} from '@/api/systemManage/adminManagement'
export default {
  name: 'editAdminUser',
  components: {},
  data() {
    return {
      tableKey: 0,
      total: 0,
      treeData: [],
      loading: false,
      listQueryAll: {
        rowBounds: {
          offset: 0,
          limit: 10
        },
        fields: [
        ],
        sort: [{
          direction: 'ASC',
          property: 'order_no'
        }],
        condition: {
          user_name:{
            property: 'user_name',
            value: '',
            type: 'LIKE'
          },
          user_loginName:{
            property: 'user_loginName',
            value: '',
            type: 'LIKE'
          }
        }
      },
      form: '',
      allTotal: 0,
      allUserData: [],
      checkedUserIds: [],
      checkedUsers: [],
      checkedUserList: [],
      checkedOffset: 0,
      multipleSelection:[],
      multipleOrgSelection:[],
      serialNumberAll: 1,
    }
  },
  filters: {
    ptFilter(target) {
      return ptKeyValue[target]
    }
  },
  created() {
    this.getList()
  },
  methods: {
    // 翻页器使用(添加人员)
    handleSizeChangeBatch(val) {
      this.listQueryAll.rowBounds.limit = val
      this.listQueryAll.isExist = 0
      compositeSelect(this.listQueryAll).then(response => {
        this.allUserData = response.data.data.records
        this.allTotal = response.data.data.count
      })
    },
    // 翻页器使用(添加人员)
    handleCurrentChangeBatch(val) {
        this.serialNumberAll = val
        this.listQueryAll.rowBounds.offset =
        this.listQueryAll.rowBounds.limit * (val - 1)
        this.listQueryAll.isExist = 0
        compositeSelect(this.listQueryAll).then(response => {
        this.allUserData = response.data.data.records
        this.allTotal = response.data.data.count
      })
    },
    getList(flag) {
        if (flag ==='find') {
          this.checkedUsers = []
          this.listQueryAll.rowBounds.offset = 0
          this.serialNumberAll =1
          this.allTotal =0
        }
        this.listQueryAll.isExist = 0

        if (flag ==='insert') {
          if ((this.listQueryAll.rowBounds.offset == (this.allTotal - 1)
          || this.allUserData.length == this.multipleSelection.length)
           && this.listQueryAll.rowBounds.offset >= this.listQueryAll.rowBounds.limit) {
            if (this.serialNumberAll * this.listQueryAll.rowBounds.limit >= this.allTotal) {
              this.listQueryAll.rowBounds.offset -=this.listQueryAll.rowBounds.limit
              this.serialNumberAll -=1
            }
          }
        }

        compositeSelect(this.listQueryAll).then(response => {
          this.allUserData = response.data.data.records
          this.allTotal = response.data.data.count
        })
    },
    closeSelf() {
      this.$emit('changeCloseDialog')
    },
   selectionChange(val,row) {
     this.checkedUsers=[];
     this.checkedUserIds=[];
     if(val){
       for(var i in val){
           var tempUserRoleOrg = {
             userId: ''
           }
          tempUserRoleOrg.userId = val[i].user_id
          this.checkedUsers.push(tempUserRoleOrg)
          this.checkedUserIds.push(val[i].user_id)
       }
     }
     this.checkedUserList = [];
     this.checkedUserList = val;
   },
   selectionValChange(val) {
    this.multipleSelection = val
   },
    insertList(flag) {
      if (this.checkedUsers.length > 0) {
        batchInsertItemUser(this.checkedUsers).then(response => {
          // 未选人员
          this.getList(flag)
          this.$message({
            message: '批量添加成功',
            type: 'success'
          })
        })
         this.checkedUsers=[];
         this.checkedUserIds=[];
       } else {
          this.$message({
            message: '请选择添加人员。',
            type: 'warning'
          })
      }
    },
   handleReset() {
     this.checkedUsers = []
     this.listQueryAll.condition.user_loginName.value = ''
     this.listQueryAll.condition.user_name.value = ''
     this.listQueryAll.rowBounds.offset = 0
     this.serialNumberAll = 1
     this.total = 0
     this.getList()
   }
  }
}
</script>

<style scoped>
.treeArea {
    max-height: 66vh;
    overflow-y: auto;
}
</style>
